<template>
  <el-container class="Home_con">
    <!-- 顶部导航栏 -->
    <el-header class="home_header">
      <div>
        <img src="../assets/heima.png" alt="" style="margin-right: 15px" />
        电商后台管理系统
      </div>
      <el-button class="home_btn" type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 内容 -->
    <el-container class="home_container">
      <!-- 内容侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'" class="con_left">
        <!-- 折叠按钮 -->
        <div class="toggle-button" @click="togglecollapse">|||</div>
        <!-- 侧边菜单栏区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          :unique-opened="true"
          active-text-color="#409eff"
          :collapse="iscollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
        <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单内容 -->
            <template slot="title">
              <!-- 图片 -->
              <i :class="iconsobj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+sublist.path"
              v-for="sublist in item.children"
              :key="sublist.id"
              @click="saveNavState('/'+sublist.path)"
            >
              <!-- 二级菜单内容 -->
              <template slot="title">
                <!-- 图片 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ sublist.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容响应 -->
      <el-main class="con_right">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsobj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      iscollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    togglecollapse() {
      this.iscollapse = !this.iscollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style scoped>
.Home_con {
  height: 100%;
}
.home_header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  width: 100%;
  color: white;
  font-size: 24px;
}
.home_header div {
  display: flex;
  align-items: center;
}
.home_container {
  background-color: #0ff;
  width: 100%;
}
/* 内容侧边栏 */
.con_left {
  background-color: #333744;
}
/* 内容 */
.con_right {
  background-color: #eaedf1;
  overflow: auto;
  height: 877px;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border: 0;
}
.toggle-button {
  background-color: #4a5064;
  color: white;
  text-align: center;
  line-height: 26px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
